package torres.view;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

import torres.RunGame;
import torres.controller.Controller;
import torres.map.World;

@SuppressWarnings("serial")
public class MainView extends JFrame {
	
	private static final MainView instance = new MainView();
	private AreaViewPort areaViewPort;
	private AreaViewPort statusViewPort;
	private InventoryView inventoryView;
	private AbilityMenuView abilityMenuView;
	private ActionMenuView actionMenuView;
	private DialogueMenuView dialogueMenuView;
	private SkillMenuView skillMenuView;
	private ShopMenuView shopMenuView;
	private PurchaseMenuView purchaseMenuView;
	private static String name = "LlamAdventure by Team Torres";
	
	private GraphicsDevice device;
	
	private MainView() {
		
		super(name);
		
		GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
		device = environment.getDefaultScreenDevice();

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setUndecorated(true);
        setIgnoreRepaint(true);
        setResizable(false);
		
        areaViewPort = new TwoDAreaViewPort();
		getContentPane().add(areaViewPort, BorderLayout.CENTER);
		
		statusViewPort = new TwoDStatusViewPort();
		getContentPane().add(areaViewPort, BorderLayout.CENTER);
		
        inventoryView = new InventoryView();
        abilityMenuView = new AbilityMenuView();
        //skillPointView.addMouseListener(Controller.getInstance());//Temp variable
       // getContentPane().add(inventoryView);
        
        actionMenuView = new ActionMenuView();
        dialogueMenuView = new DialogueMenuView();
        skillMenuView = new SkillMenuView();
        shopMenuView = new ShopMenuView();
        purchaseMenuView = new PurchaseMenuView();
		
        device.setFullScreenWindow(this);
        
        Controller.addKeyListener(areaViewPort);
        Controller.addMouseListener(areaViewPort);
        
        //create listener for status view port too?
        
        this.pack();
        setVisible(true);
        
        createBufferStrategy(2);
	}
	
	public void draw() {
		
		Graphics g = this.getGraphics();

		areaViewPort.update(g);
		
		
		if(RunGame.gameLoop.getInventory() == true && RunGame.gameLoop.getPause() == true) {
			inventoryView.update(g);
		}
		if(RunGame.gameLoop.getAbilityMenu() && RunGame.gameLoop.getPause()) {
			abilityMenuView.update(g);
		}
		if(World.getInstance().getPlayer().getActionMenu()) {
			actionMenuView.update(g);
		}
		if(World.getInstance().getPlayer().getActionMenu() && RunGame.gameLoop.getActionMenu()) {
			actionMenuView.update(g);
			dialogueMenuView.update(g);
		}
		if(World.getInstance().getPlayer().getActionMenu() && RunGame.gameLoop.getActionInventoryMenu()) {
			actionMenuView.update(g);
			inventoryView.update(g);
		}
		if(World.getInstance().getPlayer().getActionMenu() && RunGame.gameLoop.getSkillMenu()) {
			actionMenuView.update(g);
			skillMenuView.update(g);
		}
		if(World.getInstance().getPlayer().getActionMenu() && RunGame.gameLoop.getShopMenu()) {
			actionMenuView.update(g);
			shopMenuView.update(g);
		}
		if(World.getInstance().getPlayer().getActionMenu() && RunGame.gameLoop.getPurchaseMenu()) {
			actionMenuView.update(g);
			shopMenuView.update(g);
			purchaseMenuView.update(g);
		}		
		statusViewPort.update(g);
		
		g.dispose();
		
		BufferStrategy strategy = this.getBufferStrategy();
		strategy.show();
		Toolkit.getDefaultToolkit().sync();
	}
	
	public Graphics getGraphics() {
		
		BufferStrategy strategy = this.getBufferStrategy();
		return strategy.getDrawGraphics();
	}
	
	public static MainView getInstance() {
		
		return instance;
	}
	
	public AreaViewPort getAreaViewPort() {
		
		return areaViewPort;
	}
	
	public AreaViewPort getStatusViewPort() {
		
		return statusViewPort;
	}
	
	public InventoryView getInventoryView() {
		
		return inventoryView;
	}
	
	public AbilityMenuView getAbilityMenuView() {
		return this.abilityMenuView;
	}
	
	public ActionMenuView getActionMenuView() {
		return actionMenuView;
	}
	
	public DialogueMenuView getDialogueMenuView() {
		return dialogueMenuView;
	}

	public SkillMenuView getSkillMenuView() {
		return skillMenuView;
	}

	public ShopMenuView getShopMenuView() {
		return shopMenuView;
	}

	public PurchaseMenuView getPurchaseMenuView() {
		return purchaseMenuView;
	}
}
